package at.ltasat.train.validation;

import java.io.File;

import at.ltasat.Configuration;

public final class ValidateClassifierConfiguration extends Configuration {

	private int variables;
	private int clauses;
	private int iterations;
	private long maxTime;
	private int size;
	private boolean isVerboseStatus, isVerboseConfig;

	{
		variables = 333;
		clauses = 10000;
		iterations = 10;
		maxTime = 1000 * 10;
		size = 100;
		isVerboseConfig = false;
		isVerboseStatus = false;
	}

	public static ValidateClassifierConfiguration get(String[] args) {
		ValidateClassifierConfiguration config = new ValidateClassifierConfiguration();
		for (String arg : args) {
			if (arg.startsWith("-")) {
				String name = arg.substring(0, arg.indexOf('='));
				String value = arg.substring(arg.indexOf('=') + 1);
				if (name.equals("-s") || name.equals("--size")) {
					config.setSize(Integer.valueOf(value));
				} else if (name.equals("-i") || name.equals("--iterations")) {
					config.setIterations(Integer.valueOf(value));
				} else if (name.equals("-t") || name.equals("--max-time")) {
					config.setMaxTime(Long.valueOf(value));
				} else if (name.equals("-c") || name.equals("--clauses")) {
					config.setClauses(Integer.valueOf(value));
				} else if (name.equals("-v") || name.equals("--variables")) {
					config.setVariables(Integer.valueOf(value));
				} else if (name.equals("-v") || name.equals("--verbose")) {
					for (String vv : value.split(",")) {
						if (vv.equals("config")) {
							config.setVerboseConfig(true);
						} else if (vv.equals("status")) {
							config.setVerboseStatus(true);
						} else {
							throw new IllegalArgumentException("Unknown verbose value: " + vv);
						}
					}
				} else {
					throw new IllegalArgumentException("Unknown property: " + name);
				}
			} else {
				config.setIn(new File(arg));
			}
		}
		return config;
	}

	public int getVariables() {
		return variables;
	}

	public int getClauses() {
		return clauses;
	}

	public int getIterations() {
		return iterations;
	}

	public long getMaxTime() {
		return maxTime;
	}

	public int getSize() {
		return size;
	}

	public boolean isVerboseConfig() {
		return isVerboseConfig;
	}

	public boolean isVerboseStatus() {
		return isVerboseStatus;
	}

	public void setVariables(int variables) {
		if (variables <= 0) {
			throw new IllegalArgumentException("Number of variables must be greater than zero!");
		}
		this.variables = variables;
	}

	public void setClauses(int clauses) {
		if (clauses <= 0) {
			throw new IllegalArgumentException("Number of clauses must be greater than zero!");
		}
		this.clauses = clauses;
	}

	public void setIterations(int iterations) {
		if (iterations <= 0) {
			throw new IllegalArgumentException("Number of iterations must be greater than zero!");
		}
		this.iterations = iterations;
	}

	public void setMaxTime(long time) {
		if (time <= 0) {
			throw new IllegalArgumentException("Time must be greater than zero!");
		}
		this.maxTime = time;
	}

	public void setSize(int size) {
		if (size < 0) {
			throw new IllegalArgumentException("Validation size must be at least zero!");
		}
		this.size = size;
	}

	public void setVerboseStatus(Boolean value) {
		this.isVerboseStatus = value;
	}

	public void setVerboseConfig(Boolean value) {
		this.isVerboseConfig = value;
	}

	@Override
	public String toString() {
		StringBuilder string = new StringBuilder();
		string.append(String.format("--verbose=%s,%s ", isVerboseConfig ? "config" : "", isVerboseStatus ? "status" : ""));
		string.append(String.format("--size=%d ", size));
		string.append(String.format("--iterations=%d ", iterations));
		string.append(String.format("--max-time=%d ", maxTime));
		string.append(String.format("--clauses=%d ", clauses));
		string.append(String.format("--variables=%d ", variables));
		if (isInFile()) {
			string.append(getInFile().getAbsolutePath());
		}
		return string.toString().trim();
	}

}
